package com.spm.librarianmanagersystem.utils;

import lombok.Data;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.HashMap;

/**
 *  @Class: FormatUtil
 *  @Author: Wu Yue
 *  @Time: 21:56 2022/4/4
 *  @Description:
 */
@Component
@Data
public class FormatUtil {

    public HashMap<Integer, String> errorMap = new HashMap<>();

    public FormatUtil() {
        this.errorMap.put(0, "Successfully");
        this.errorMap.put(1, "There is an error with the ISBN, please try again");
        this.errorMap.put(2, "Illegal date");
        this.errorMap.put(3, "The start date is earlier than today's date");
    }

    public Integer ISBNFormat(String ISBN) {
        /**
         *
         */
        return 0;
    }

    public Integer dateFormat(Integer y1, Integer m1, Integer y2, Integer m2) {

        if (y1.equals(0) && m1.equals(0) && y2.equals(0) && m2.equals(0))
            return 0;
        else if (y1 < 1900 || m1 < 1 || m1 > 12)
            return 2;
        else if ((y1 > y2 || (y1 < y2 && m1 > m2)) && (!y2.equals(0) && !m2.equals(0)))
            return 2;
        else {
            if (m1.equals(0)) {
                m1 = 1;
                Date date = new Date(y1 - 1900, m1 - 1, 1);
                System.out.println(date);
                if (date.after(new Date(System.currentTimeMillis()))) {
                    return 3;
                }
            } else {
                Date date = new Date(y1 - 1900, m1 - 1, 1);
                System.out.println(date);
                if (date.after(new Date(System.currentTimeMillis()))) {
                    return 3;
                }
            }
        }
        return 0;
    }
}
